<template>
  <div class="container">
    <el-card style="margin: 10px">
      <!-- form表单 -->
      <el-form label-position="left" inline size="small">
        <el-row type="flex" justify="space-between">
          <el-form-item label="关键字">
            <el-input
              placeholder="根据编号搜索"
              v-model="pageForm.keyword"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="hClean">清除</el-button>
            <el-button type="primary" @click="hInquiry">搜索</el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <!-- alert提示 -->
      <el-alert title="" type="info" show-icon>
        数据一共{{ total }}条
      </el-alert>
      <!-- table表格 -->
      <el-table style="margin-top: 15px" :data="randomList">
        <el-table-column prop="id" label="编号" width="230%"></el-table-column>
        <el-table-column label="题型" prop="questionType">
          <template v-slot="scope">
            {{ typeofquestionType(scope.row.questionType) }}
          </template>
        </el-table-column>
        <el-table-column label="题目编号" prop="questionIDs" width="230%">
          <template v-slot="scope">
            <el-button
              v-for="item in scope.row.questionIDs"
              class="btn"
              :key="item.number"
              @click="hPreview(item)"
              type="text"
              style="margin:0"
              >{{ item.number }}
              </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="录入时间"
          prop="addTime"
          width="200%"
        ></el-table-column>
        <el-table-column label="答题时间(s)" prop="totalSeconds">
        </el-table-column>
        <el-table-column label="正确率(%)" prop="accuracyRate">
        </el-table-column>
        <el-table-column label="录入人" prop="userName"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              type="danger" icon="el-icon-delete" circle
              @click="hDel(scope.row)"
              ></el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <!-- pag分页 -->
      <el-row type="flex" justify="end">
        <el-pagination
          background
          layout="prev,pager,next,sizes,jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageForm.pagesize"
          :total="total"
          :page="pageForm.page"
          :current-page.sync="pageForm.page"
        ></el-pagination>
      </el-row>
    </el-card>

    <!-- 预览弹窗 -->
    <el-dialog
      width="55%"
      :visible.sync="PreviewShowDialog"
      title="题目预览"
      v-if="PreviewShowDialog"
    >
      <preview
      :previewIdList="previewIdList"
      ref="refPreview"
      @dialogClose="PreviewShowDialog = false"/>
    </el-dialog>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'

// 导入子组件
import preview from '../components/preview.vue'

// 导入枚举信息
import { questionType } from '@/api/hmmm/constants.js'
const sta = questionType.reduce((acc, cur) => {
  acc[cur.value] = cur.label
  return acc
}, {})
export default {
  components: {
    preview
  },
  data () {
    return {
      randomList: [],
      pageForm: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      total: 5,
      showDialog: false,
      videoShowDialog: false,
      PreviewShowDialog: false,

      // 父传子-video
      previewIdList: {},
      // 回填数据
      formData: {}
    }
  },
  created () {
    this.loadRandom()
  },
  methods: {
    // 更新成功
    hsuccess () {
      this.pageForm.page = 1
      // 刷新页面
      this.loadRandom()
      // 关闭弹窗
      this.showDialog = false
    },
    // 删除弹窗提醒
    hDel (row) {
      this.$confirm('此操作将永久删除该学科,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDel(row)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 做删除
    async doDel (row) {
      try {
        const res = await removeRandoms(row)
        // console.log(res)
        if (res.status === 200) {
          this.$message.success('删除成功')
        }
      } catch (error) {
        this.$message.error(error.message)
      }

      // 解决删除后当前页面数据为空bug
      if (this.randomList.length === 1) {
        let page = this.pageForm.page
        page--

        page = Math.max(1, page)
        this.pageForm.page = page
      }
      this.loadRandom()
    },
    // 预览题目弹窗
    async hPreview (data) {
      // const res = await detail(data)
      // console.log('详情', res)
      this.PreviewShowDialog = true
      this.previewIdList = data
      this.$nextTick(() => {
        this.$refs.refPreview.getPreviewList()
      })
    },

    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      // 回到第一页
      this.pageForm.page = 1
      this.pageForm.pagesize = val

      // 刷新
      this.loadRandom(this.pageForm)
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.pageForm.page = val

      // 刷新
      this.loadRandom(this.pageForm)
    },

    // 状态格式转换
    typeofquestionType (questionType) {
      return sta[questionType]
    },

    // 获取目录列表
    async loadRandom () {
      try {
        const res = await randoms(this.pageForm)
        console.log(res)
        this.randomList = res.data.items
        this.total = res.data.counts
      } catch (error) {}
    },

    // 清空表单
    hClean () {
      this.pageForm = {
        page: 1,
        pagesize: 10,
        title: ''
      }
    },

    // 点击查询
    hInquiry () {
      this.pageForm.page = 1
      this.loadRandom()
    },

    // 连续序号
    pageAdd (index) {
      return (this.pageForm.page - 1) * this.pageForm.pagesize + index + 1
    }
  }
}
</script>

<style scoped lang='scss'>
.btn {
  display: flex;
  justify-content: start;
}
</style>
